import tkinter as tk
from PIL import Image, ImageTk
from tkinter import ttk,messagebox

import requests

# 示例问题数据
questions = [
    {
        "question": "What is the capital of France?",
        "options": ["Berlin", "Madrid", "Paris", "Rome"],
        "answer": "Paris"
    },
    {
        "question": "Which planet is known as the Red Planet?",
        "options": ["Earth", "Mars", "Jupiter", "Saturn"],
        "answer": "Mars"
    },
    {
        "question": "Who wrote 'To Kill a Mockingbird'?",
        "options": ["Harper Lee", "J.K. Rowling", "Ernest Hemingway", "Mark Twain"],
        "answer": "Harper Lee"
    }
]

class IncompletedQuestionnaireFrame(tk.Toplevel):
    def __init__(self, parent,questionnaires):
        super().__init__()

        # 初始化数据

        self.parent = parent
        self.title("未完成问卷详情")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 2)
        root_height = int(screen_height / 2)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))



        # 使用 Pillow 加载图像
        image = Image.open("Image/return.png")
        # 调整图像大小
        new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        resized_image = image.resize(new_size, Image.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        self.back_button = tk.Button(self, image=photo, text='返回', font=("宋体", 15), compound='left',
                                     command=self.back_to_main)
        self.back_button.photo = photo  # 保存图像对象的引用
        self.back_button.place(relx=0.01, rely=0.01, relwidth=0.15, relheight=0.1)


        # TODO TreeView
        self.questionnaires = questionnaires
        self.answers = {}

        self.setup_ui()
        # self.load_questionnaires()

    def setup_ui(self):
        # self.main_frame = tk.Frame(self)
        # self.main_frame.grid(row=0, column=0, sticky='nsew')

        # # Configure the root grid
        # self.grid_rowconfigure(0, weight=1)
        # self.grid_columnconfigure(0, weight=1)
        #
        # # Configure the main_frame grid
        # self.main_frame.grid_rowconfigure(0, weight=1)
        # self.main_frame.grid_columnconfigure(0, weight=1)

        # 创建Canvas和Scrollbar
        canvas = tk.Canvas(self)
        scrollbar = tk.Scrollbar(self, orient="vertical", command=canvas.yview)
        scrollable_frame = tk.Frame(canvas)

        scrollable_frame.bind(
            "<Configure>",
            lambda e: canvas.configure(
                scrollregion=canvas.bbox("all")
            )
        )

        canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
        canvas.configure(yscrollcommand=scrollbar.set)

        canvas.place(relx=0.01, rely=0.2, relwidth=0.97, relheight=0.5)
        scrollbar.place(relx=0.96, rely=0.2, relwidth=0.03, relheight=0.5)

        selected_options = {}  # 存储每个问题的selected_option变量

        for i, q in enumerate(questions):
            question_label = tk.Label(scrollable_frame, text=f"Q{i + 1}: {q['question']}")
            question_label.pack(anchor="w", padx=10, pady=5)

            # 创建独立的变量名称
            var_name = f"selected_option{i + 1}"
            selected_options[var_name] = tk.StringVar(value=q["answer"])  # 自动选中正确答案

            for option in q["options"]:
                radio_button = tk.Radiobutton(scrollable_frame, text=option, variable=selected_options[var_name],
                                              value=option)
                radio_button.pack(anchor="w", padx=20)

            answer_label = tk.Label(scrollable_frame, text=f"Answer: {q['answer']}", fg="green")
            answer_label.pack(anchor="w", padx=10, pady=5)

            # 打印设置的选项值，确认选中正确答案
            print(f"Selected answer for Q{i + 1}: {selected_options[var_name].get()}")  # 获取实际值

        self.submit_button = tk.Button(self, text="提交", command=self.submit_key_function)
        self.submit_button.place(relx=0.7, rely=0.7, relwidth=0.1, relheight=0.1)

    # 返回键功能
    def back_to_main(self):
        self.destroy()
        self.parent.deiconify()

    # 提交键功能
    def submit_key_function(self):
        self.on_submit()

    def on_submit(self):
        messagebox.showinfo('Info', '提交完成')